package com.woniuxy.oa.web;

import com.woniuxy.oa.entity.Emp;
import com.woniuxy.oa.entity.Message;
import com.woniuxy.oa.entity.ResponseVO;
import com.woniuxy.oa.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("message")
@RestController
@CrossOrigin("*")
public class MessageController {
    @Autowired
    private MessageService messageService;

    /**
     * 消息列表
     *
     * @param message
     * @return
     */
    @RequestMapping("list")
    public ResponseVO list(@RequestBody(required = false) Message message) {
        return new ResponseVO("200", "查询成功", messageService.getPageBean(message, message.getPage()));
    }

    /**
     * 删除消息
     *
     * @param ids
     * @return
     */
    @GetMapping("del/{ids}")
    public ResponseVO del(@PathVariable String[] ids) {
        for (String id : ids) {
            System.out.println("id: " + id);
        }
        messageService.del(ids);
        return new ResponseVO("200", "删除成功", null);
    }

    /**
     * 已读消息
     *
     * @param id
     * @return
     */
    @GetMapping("updateRead/{id}")
    public ResponseVO updateRead(@PathVariable String id) {
        messageService.updateRead(id);
        return new ResponseVO("200", "删除成功", null);
    }

    /**
     * 发送消息
     *
     * @param message
     * @return
     */
    @RequestMapping("add")
    public ResponseVO add(@RequestBody Map data) {
//        message.setSendId();
//        messageService.add(message, ids);
        Message message = new Message();
        message.setSendId((String) data.get("empid"));
        message.setTitle((String) data.get("title"));
        message.setContent((String) data.get("content"));
        List<String> ids = (List<String>) data.get("ids");
        messageService.add(message, ids);
        return new ResponseVO("200", "发送成功", null);
    }
}
